package com.sky.loveshop.controller;

import com.sky.loveshop.entity.Result;
import com.sky.loveshop.entity.aliPay.OrderRequest;
import com.sky.loveshop.entity.aliPay.PreCreatResponse;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.service.AliPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/9/1 11:19
 */
@Slf4j
@RequestMapping("/aliPay")
@RestController
public class AliPayController {

    @Autowired
    private AliPayService aliPayService;

    /**
     * 预下单测试
     */
    @RequestMapping(value = "/testOrderPay")
    public void testOrderPay(HttpServletResponse response) {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("orderId", UUID.randomUUID().toString());
            param.put("goodsName", "情趣空间");
            param.put("fee", 0.01);
            aliPayService.testOrderPay(param, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 支付宝订单支付异步通知回调
     */
    @PostMapping("/payNotify")
    public String payNotify(HttpServletRequest request) {
        log.info("orderPayNotifyCallBack payNotify 支付宝支付异步通知回调");
        return aliPayService.orderPayNotify(request);
    }

    /**
     * 预下单生成二维码(当面付-用户扫商家) 测试
     */
    @GetMapping("/preCreatRequest")
    public Result<PreCreatResponse> preCreatRequest(HttpServletResponse response) throws IOException {
        PreCreatResponse preCreatResponse = aliPayService.preCreatRequest();
        // 不能直接返回图片流，得额外加上订单编号给到前端进行定时1秒查一次是否已经支付成功并关闭二维码
        //QrCodeUtil.generate(qrCode, 300, 300, "png", response.getOutputStream());
        return Result.success(preCreatResponse);

    }

    /**
     * 预下单生成二维码(当面付-用户扫商家) 正式
     */
    @PostMapping("/preCreatOrderAndDetail")
    public Result<PreCreatResponse> preCreatOrderAndDetail(@RequestBody OrderRequest request, HttpServletResponse response) throws IOException, BizException {
        PreCreatResponse preCreatResponse = aliPayService.preCreatOrderAndDetail(request);
        // 不能直接返回图片流，得额外加上订单编号给到前端进行定时1秒查一次是否已经支付成功并关闭二维码
        //QrCodeUtil.generate(qrCode, 300, 300, "png", response.getOutputStream());
        return Result.success(preCreatResponse);
    }

    /**
     * 统一收单交易查询
     */
    @GetMapping("/aliPayTradeQuery")
    public Result<String> aliPayTradeQuery(String orderNo) {
        return Result.success(aliPayService.aliPayTradeQuery(orderNo));
    }

    /**
     * 统一收单交易关闭接口
     * 用于交易创建后，用户在一定时间内未进行支付，可调用该接口直接将未付款的交易进行关闭。
     *
     * @param orderNo 商户订单号(自己系统生成的订单号)
     * @return
     */
    @GetMapping("/aliPayTradeClose")
    public Result<Integer> aliPayTradeClose(String orderNo) {
        return Result.success(aliPayService.aliPayTradeClose(orderNo));
    }

    /**
     * 提供给前端定时1秒查询一下订单状态是否已经支付 订单支付状态(0未支付|1已支付|2支付失败)
     */
    @GetMapping("/aliPayTradeQueryOrderStatus")
    public Result<Integer> aliPayTradeQueryOrderStatus(String orderNo) {
        return Result.success(aliPayService.aliPayTradeQueryOrderStatus(orderNo));
    }


}
